package cn.wjh.business.service.impl;

import cn.wjh.business.domain.Banner;
import cn.wjh.business.mapper.BannerMapper;
import cn.wjh.business.service.IBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 轮播图Service业务层处理
 *
 * @author jiraWu
 * @date 2023-10-22
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements IBannerService {
	@Resource
	private BannerMapper bannerMapper;

	/**
	 * 查询轮播图
	 *
	 * @param bannerId 轮播图主键
	 * @return 轮播图
	 */
	@Override
	public Banner selectBannerByBannerId(Long bannerId) {
		return bannerMapper.selectBannerByBannerId(bannerId);
	}

	/**
	 * 查询轮播图列表
	 *
	 * @param banner 轮播图
	 * @return 轮播图
	 */
	@Override
	public List<Banner> selectBannerList(Banner banner) {
		return bannerMapper.selectBannerList(banner);
	}

	/**
	 * 新增轮播图
	 *
	 * @param banner 轮播图
	 * @return 结果
	 */
	@Override
	public int insertBanner(Banner banner) {
		long count = this.count();
		if (count > 5) throw new ServiceException("只允许上传五张轮播图");
		banner.setCreateTime(DateUtils.getNowDate());
		banner.setUpdateTime(DateUtils.getNowDate());
		banner.setCreateBy(SecurityUtils.getUsername());
		return bannerMapper.insertBanner(banner);
	}

	/**
	 * 修改轮播图
	 *
	 * @param banner 轮播图
	 * @return 结果
	 */
	@Override
	public int updateBanner(Banner banner) {
		banner.setUpdateTime(DateUtils.getNowDate());
		banner.setUpdateBy(SecurityUtils.getUsername());
		return bannerMapper.updateBanner(banner);
	}

	/**
	 * 批量删除轮播图
	 *
	 * @param bannerIds 需要删除的轮播图主键
	 * @return 结果
	 */
	@Override
	public int deleteBannerByBannerIds(Long[] bannerIds) {
		return bannerMapper.deleteBannerByBannerIds(bannerIds);
	}

	/**
	 * 删除轮播图信息
	 *
	 * @param bannerId 轮播图主键
	 * @return 结果
	 */
	@Override
	public int deleteBannerByBannerId(Long bannerId) {
		return bannerMapper.deleteBannerByBannerId(bannerId);
	}
}
